package tool;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @Description:
 * @Author: yjj
 * @Date: 2025/8/18 14:52
 */
public class Main6 {
    public static void main(String[] args) {

        String s = "{\"method\":\"ResourceBackListener.process\",\"requestData\":\"\\\"{\\\\\\\"commandCategoryId\\\\\\\":4833146901715356113,\\\\\\\"instanceId\\\\\\\":4823634116090372483,\\\\\\\"isError\\\\\\\":\\\\\\\"true\\\\\\\",\\\\\\\"requestId\\\\\\\":\\\\\\\"d1d7256c-cc83-4f88-a3d7-1b0b242800ce\\\\\\\",\\\\\\\"serverType\\\\\\\":\\\\\\\"1834\\\\\\\",\\\\\\\"requestParams\\\\\\\":\\\\\\\"{\\\\\\\\\\\\\\\"tenant_id\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"4813278056586346510\\\\\\\",\\\\\\\\\\\\\\\"cloud_code\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"wocloud\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"region_id\\\\\\\\\\\\\\\":4813278056586346496,\\\\\\\\\\\\\\\"action\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"instanceBackup.wal.create\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"type\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"pgrds\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"request_id\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"d1d7256c-cc83-4f88-a3d7-1b0b242800ce\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"url_param\\\\\\\\\\\\\\\":{\\\\\\\\\\\\\\\"instanceId\\\\\\\\\\\\\\\":4822198789559607704,\\\\\\\\\\\\\\\"rds_id\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"ebb12984-76b1-417e-a918-4288acc3443b\\\\\\\\\\\\\\\"}}\\\\\\\",\\\\\\\"responseParams\\\\\\\":\\\\\\\"{\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\":422,\\\\\\\\\\\\\\\"data\\\\\\\\\\\\\\\":{\\\\\\\\\\\\\\\"unprocessedEntity\\\\\\\\\\\\\\\":{\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\":422,\\\\\\\\\\\\\\\"message\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"No WAL file(s) to backup from instance ebb12984-76b1-417e-a918-4288acc3443b.\\\\\\\\\\\\\\\"}},\\\\\\\\\\\\\\\"requestId\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"d1d7256c-cc83-4f88-a3d7-1b0b242800ce\\\\\\\\\\\\\\\"}\\\\\\\",\\\\\\\"methodCode\\\\\\\":\\\\\\\"instanceBackup.wal.create\\\\\\\"}\\\"\",\"requestMethod\":\"QUEUE\",\"responseData\":\"null\",\"serviceCode\":\"server\",\"url\":\"resource_back_queue\"}";

        String ss = "{\\\"commandCategoryId\\\":4833146901715356113,\\\"instanceId\\\":4823634116090372483,\\\"isError\\\":\\\"true\\\",\\\"requestId\\\":\\\"d1d7256c-cc83-4f88-a3d7-1b0b242800ce\\\",\\\"serverType\\\":\\\"1834\\\",\\\"requestParams\\\":\\\"{\\\\\\\"tenant_id\\\\\\\":\\\\\\\"4813278056586346510\\\\\\\",\\\\\\\"cloud_code\\\\\\\":\\\\\\\"wocloud\\\\\\\",\\\\\\\"region_id\\\\\\\":4813278056586346496,\\\\\\\"action\\\\\\\":\\\\\\\"instanceBackup.wal.create\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"pgrds\\\\\\\",\\\\\\\"request_id\\\\\\\":\\\\\\\"d1d7256c-cc83-4f88-a3d7-1b0b242800ce\\\\\\\",\\\\\\\"url_param\\\\\\\":{\\\\\\\"instanceId\\\\\\\":4822198789559607704,\\\\\\\"rds_id\\\\\\\":\\\\\\\"ebb12984-76b1-417e-a918-4288acc3443b\\\\\\\"}}\\\",\\\"responseParams\\\":\\\"{\\\\\\\"code\\\\\\\":422,\\\\\\\"data\\\\\\\":{\\\\\\\"unprocessedEntity\\\\\\\":{\\\\\\\"code\\\\\\\":422,\\\\\\\"message\\\\\\\":\\\\\\\"No WAL file(s) to backup from instance ebb12984-76b1-417e-a918-4288acc3443b.\\\\\\\"}},\\\\\\\"requestId\\\\\\\":\\\\\\\"d1d7256c-cc83-4f88-a3d7-1b0b242800ce\\\\\\\"}\\\",\\\"methodCode\\\":\\\"instanceBackup.wal.create\\\"}";

        // 4. 打印结果
        JSONObject jsonObject = parseEscapedJsonString(s);
        System.out.println(jsonObject.toJSONString());
    }

    public static JSONObject parseEscapedJsonString(String input) {
        if (input == null || (input = input.trim()).isEmpty()) return null;
        try {
            // Step 1: 修复转义
            String fixed = input
                    .replace("\\\\\\\\\\", "\\")
                    .replace("\\\\\\\\", "\\")
                    .replace("\\\\\\", "\\")
                    .replace("\\\\", "\\")
                    .replace("\\\"", "\"")
                    .replace("\r", "")
                    .replace("\n", "");

            System.out.println("🔧 修复后: " + fixed); // 🔍 调试用

            // Step 2: 解析
            JSONObject outer = JSON.parseObject(fixed);

            // Step 3: 安全处理嵌套字段（只处理字符串类型的）

            return outer;

        } catch (Exception e) {
            System.err.println("❌ 解析失败: " + e.getMessage());
            return null;
        }
    }
}
